package com.mj.mafeed.activity;

import com.mj.mafeed.R;
import com.mj.mafeed.service.MemberService;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.util.SPUtils;
import com.mj.mafeed.widget.LoadingDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 个人资料
 * @author zhaominglei
 * @date 2014-12-19
 * 
 */
public class ProfileActivity extends Activity implements OnClickListener{
	@SuppressWarnings("unused")
	private static final String TAG = ProfileActivity.class.getSimpleName();
	
	private ImageView goHome;
	private TextView accountView; //账户
	private EditText emailText;
	private EditText nameText;
	private Button saveBtn;
	private Button cancelBtn;
	private LoadingDialog loadingDialog; //加载框
	private String referer; //来自那个链接
	private String username; //用户名
	private String sign; //签名
	private String email; //邮箱
	private String name; //姓名
	private String srcEmail; //原邮箱
	private String srcName; //原姓名
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) findViewById(R.id.profile_gohome);
		accountView = (TextView) findViewById(R.id.profile_account);
		emailText = (EditText) findViewById(R.id.profile_email_edt);
		nameText = (EditText) findViewById(R.id.profile_name_edt);
		saveBtn = (Button) findViewById(R.id.profile_save);
		cancelBtn = (Button) findViewById(R.id.profile_cancel);
		
		goHome.setOnClickListener(this);
		saveBtn.setOnClickListener(this);
		cancelBtn.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		
		referer = getIntent().getStringExtra("referer");
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		srcEmail = (String)SPUtils.get(this, "email", "");
		srcName = (String)SPUtils.get(this, "name", "");
		if (srcEmail != null && !srcEmail.equals("")) {
			emailText.setText(srcEmail);
		}
		if (srcName != null && !srcName.equals("")) {
			nameText.setText(srcName);
		}
		accountView.setText(username);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.profile_gohome:
			intent = new Intent(getApplicationContext(), SettingActivity.class);
			intent.putExtra("referer", "ProfileActivity");
			startActivity(intent);
			ProfileActivity.this.finish();
			break;
			
		case R.id.profile_save:
			save(v);
			break;
			
		case R.id.profile_cancel:
			intent = new Intent(getApplicationContext(), SettingActivity.class);
			intent.putExtra("referer", "ProfileActivity");
			startActivity(intent);
			ProfileActivity.this.finish();
			break;
			
		default:
			break;
		}
	}

	/**
	 * 保存
	 */
	private void save(View v) {
		email = emailText.getText().toString();
		name = nameText.getText().toString();
		if (email == null || email.equals("")) {
			Toast.makeText(v.getContext(), R.string.profile_check_error1, Toast.LENGTH_SHORT).show();
			return;
		}

		if (email.equals(srcEmail) && srcName.equals(name)) {
			ProfileActivity.this.finish();
		} else {
			if (NetUtils.isConnected(getApplicationContext())) {
				loadingDialog.show();
				new ProfileSaveAsyncTask().execute("");
			} else {
				Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	public void onBackPressed() {
		intent = new Intent(getApplicationContext(), SettingActivity.class);
		intent.putExtra("referer", "ProfileActivity");
		startActivity(intent);
		ProfileActivity.this.finish();
	}

	/**
	 * 修改个人资料
	 */
	public class ProfileSaveAsyncTask extends AsyncTask<String, String, Boolean> {

		@Override
		protected Boolean doInBackground(String... params) {
			return new MemberService().updateProfile(referer, email, name, username, sign);
		}
		@Override
		protected void onPostExecute(Boolean result) {
			loadingDialog.dismiss();
			if (result) {
				SPUtils.remove(getApplicationContext(), "email");
				SPUtils.remove(getApplicationContext(), "email");
				SPUtils.put(getApplicationContext(), "email", email);
				SPUtils.put(getApplicationContext(), "name", name);
				Intent intent = new Intent(getApplicationContext(), MainActivity.class);
				intent.putExtra("referer", "ProfileActivity");
				startActivity(intent);
				ProfileActivity.this.finish();
				Toast.makeText(getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getApplicationContext(), R.string.save_error, Toast.LENGTH_SHORT).show();
			}
		}
	}
}
